package com.tony.juc;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: BlockingQueueDemo001
 * @Auther: 皮蛋布丁
 * @Date: 2022/04/16/15:39
 * @Description: 阻塞队列
 */
public class BlockingQueueDemo001 {

    public static void main(String[] args) {

        /*//每一个队列在启用的时候必须明确的设置其保存的数据的个数
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(5);//数组阻塞队列
        //如果要想观察到队列的使用，最佳的做法就是生产者与消费者的处理模型
        for (int i = 0; i < 10; i++) { //创建循环
            final int temp = i;
            new Thread(()->{
                for (int j = 0; j < 100; j++) { //持续生产
                    try {
                        TimeUnit.SECONDS.sleep(2); //生产慢
                        String msg = "{ID = }" + temp + "-" + j + "}皮蛋布丁";
                        queue.put(msg); //向队列中添加数据
                        System.out.printf("【%s】%s%n",Thread.currentThread().getName(),msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            },"生产者_" + i).start();
        }
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                while (true) { //持续性的通过队列抓取数据
                    try {
                        TimeUnit.SECONDS.sleep(2); //消费慢
                        System.out.printf("〖%s〗%s", Thread.currentThread().getName(), queue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"消费者_" + i).start();
        }*/


        /*//每一个队列在启用的时候必须明确的设置其保存的数据的个数
        BlockingQueue<String> queue = new LinkedBlockingQueue<>(5);//数组阻塞队列
        //如果要想观察到队列的使用，最佳的做法就是生产者与消费者的处理模型
        for (int i = 0; i < 10; i++) { //创建循环
            final int temp = i;
            new Thread(()->{
                for (int j = 0; j < 100; j++) { //持续生产
                    try {
                        TimeUnit.SECONDS.sleep(2); //生产慢
                        String msg = "{ID = }" + temp + "-" + j + "}皮蛋布丁";
                        queue.put(msg); //向队列中添加数据
                        System.out.printf("【%s】%s%n",Thread.currentThread().getName(),msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            },"生产者_" + i).start();
        }
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                while (true) { //持续性的通过队列抓取数据
                    try {
                        TimeUnit.SECONDS.sleep(2); //消费慢
                        System.out.printf("〖%s〗%s", Thread.currentThread().getName(), queue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"消费者_" + i).start();
        }*/


        /*//每一个队列在启用的时候必须明确的设置其保存的数据的个数
        BlockingQueue<String> queue = new PriorityBlockingQueue<>(5);//数组阻塞队列
        //如果要想观察到队列的使用，最佳的做法就是生产者与消费者的处理模型
        for (int i = 0; i < 10; i++) { //创建循环
            final int temp = i;
            new Thread(()->{
                for (int j = 0; j < 100; j++) { //持续生产
                    try {
                        TimeUnit.SECONDS.sleep(2); //生产慢
                        String msg = "{ID = }" + temp + "-" + j + "}皮蛋布丁";
                        queue.put(msg); //向队列中添加数据
                        System.out.printf("【%s】%s%n",Thread.currentThread().getName(),msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            },"生产者_" + i).start();
        }
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                while (true) { //持续性的通过队列抓取数据
                    try {
                        TimeUnit.SECONDS.sleep(2); //消费慢
                        System.out.printf("〖%s〗%s", Thread.currentThread().getName(), queue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"消费者_" + i).start();
        }*/


        //每一个队列在启用的时候必须明确的设置其保存的数据的个数
        BlockingQueue<String> queue = new SynchronousQueue<String>();//数组阻塞队列
        //如果要想观察到队列的使用，最佳的做法就是生产者与消费者的处理模型
        for (int i = 0; i < 10; i++) { //创建循环
            final int temp = i;
            new Thread(()->{
                for (int j = 0; j < 100; j++) { //持续生产
                    try {
                        TimeUnit.SECONDS.sleep(2); //生产慢
                        String msg = "{ID = }" + temp + "-" + j + "}皮蛋布丁";
                        queue.put(msg); //向队列中添加数据
                        System.out.printf("【%s】%s%n",Thread.currentThread().getName(),msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            },"生产者_" + i).start();
        }
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                while (true) { //持续性的通过队列抓取数据
                    try {
                        TimeUnit.SECONDS.sleep(2); //消费慢
                        System.out.printf("〖%s〗%s", Thread.currentThread().getName(), queue.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"消费者_" + i).start();
        }

    }
}
